********** ********** ********** ********** ********** ********** **********
********** “In good times and in bad, in sickness and in health"  **********
********** Authors: Regina Gerlich & Tobias Wolbring			  **********
********** 											   			  **********
********** SOEPlong v33								   			  **********
********** 											   			  **********
********** 											   			  **********
********** Stand: 07|06|2020						   			  **********
********** ********** ********** ********** ********** ********** ********** 

*prepare data for merging
use pbrutto.dta, clear
	keep cid hid pid syear geburt sex pnat lint pnrakt stell pzug hhnrold	 ///
		 pnrold pzugv auszugm auszugj einzugm einzugj pzugm pzugj part zuhau ///
		 abhau sample1 stell1
	sort hid pid syear 
save pbrutto_short.dta

use pl.dta, clear
	sort hid pid syear
	drop if hid==0
save pl_sorted.dta, replace

use hl.dta, clear
	sort hid syear
save hl_sorted.dta

* load master data set
use pbrutto_sorted.dta, clear

* merge
merge m:1 hid pid syear using pl_sorted.dta
	sort hid pid syear 
	keep if _merge==3
	drop _merge

	sort hid syear
merge m:1 hid syear using hl_sorted.dta
	keep if _merge==3
	drop _merge

* merge futher data sets for control
	/*
	relevant variable in data set ppfad: todjahr (if respondent died)
	relevant variable in data set pgen:	pgfamstd (family status)
	*/
merge m:1 pid using ppfad.dta
	sort hid pid syear 
	drop _merge
	
merge m:1 hid pid syear using pgen_sorted.dta
	sort hid pid syear 
	drop _merge
	
* Time-Series Data
tsset pid syear

save happypflege_v33_2020.dta, replace

* drop years before 1999; before it wasn´t asked for the carer within the HH 
drop if syear<1999	

* duration SOEP participation (individual level)
bysort pid: egen soep_anfang = min(syear)
bysort pid: egen soep_ende 	 = max(syear)
gen dauer_soep = soep_ende-soep_anfang +1
	lab var dauer_soep "duration SOEP participation (individual level)"
* drop observations with only 1 wave
drop if dauer_soep==1

* decode all Missings	
mvdecode _all, mv(-8=.f\-3=.u\-2=.t\-1=.k\-5=.w)
	// .t = does not apply
	// .k = no answer
	// .f = not in the questionnaire
	// .u = not plausible
	// .w = not clear

* drop variables that only constists out of missings	
dropmiss, force
* drop observations without identifying HH-number 
drop if hid==.

save happypflege_v33_2020_reduced.dta, replace


*** Variables ***
* recode gender
gen male 	 = 1 if sex==1
replace male = 0 if sex==2
	lab var male "Gender"
		lab define male ///
			0 "women"  ///
			1 "men"
	lab val male male

* age
gen age = syear-geburt
	lab var age "Age"

* current health status
gen gesund = (ple0008-5)*(-1)
	lab var gesund "Health status"
		lab define gesund		///
			0 "bad"				///
			1 "poor"			///
			2 "satisfactory"	///
			3 "good"			///
			4 "very good"			
	lab val gesund gesund
	
* current employment status
clonevar erwerb = plb0022
	lab var erwerb "Employment status"
		lab define erwerb							///
			1 "full time"							///
			2 "part time"							///
			3 "vocational training"					///
			4 "marginaly employed"					///
			5 "near retirement, 0 working hours"	///
			9 "not employed"			
	lab val erwerb erwerb

* max. number of persons in the household
bysort syear hid: egen anzahl_hh = max(pnrakt) 
	lab var anzahl_hh "max. number of persons in the HH"
	
* HH-income all together
clonevar income_hh = hlc0005

* HH-income in average per person
gen pp_income_hh = income_hh/anzahl_hh

* Life satisfaction
clonevar happy = plh0182

** identify caregiving households **
* hh with person in need of care
gen 	pf = 0 if hlf0291==2
replace pf = 1 if hlf0291==1
replace pf = .k if hlf0291==.k
	lab var pf "person in need of care exists in this year"
		lab define yn ///
			0 "no"  ///
			1 "yes"
	lab val pf yn

* hh without any persons in need of care
bysort hid (syear): egen help_pf = total(pf)
gen nevercare =(help_pf==0)
	lab var nevercare "HH had never cases of care"
		lab val nevercare yn

** bring caregivers and caretakers together
* main caregiver person number
gen pfleger_pnr = hlf0319 if hlf0319!=0

* caretaker person number
gen gepflegter_pnr = hlf0292 if hlf0292!=0

** xtset data **
xtset pid syear

** identify caretaker
* link to PNR - caretaker
gen gepflegter_id=pid if gepflegter_pnr==pnrakt & gepflegter_pnr!=0 & pnrakt!=.
sort pid syear
* fill gaps if only 1 year without information
bysort pid: replace gepflegter_id = pid if L.gepflegter_id==pid & ///
				  F.gepflegter_id==pid	
gen pf2=pf
sort pid syear
bysort pid: replace pf2 = 1 if L.pf==1 & F.pf==1

* caretaker harmonize within the HH
bysort hid: egen gepflegter_id_hh_helpi1 = max(gepflegter_id)
bysort hid: egen gepflegter_id_hh_helpi2 = min(gepflegter_id)
		 
* if caretaker changes
gen wechselpflege = (gepflegter_id_hh_helpi1!=gepflegter_id_hh_helpi2)
replace wechselpflege = . if gepflegter_id_hh_helpi1==.
drop if hid==723037
drop if wechselpflege==1
* drop HH with caregiver but without caretaker		
drop if wechselpflege==0 & nevercare==1
		
* caregiver harmonize within the HH
bysort hid: egen gepflegter_all = min(gepflegter_id)
bysort hid: egen gepflegter_all1 = max(gepflegter_id)

* consider gaps
sort pid syear
bysort pid: gen luecken = (pf==1 & F.pf==0 & F2.pf==1 |	///
						  pf==1 & F.pf==0 & F2.pf==0 & F3.pf==1 | ///
						  pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==1 | ///
			   pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==1 | ///
	pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==1 | ///  
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==0 & F14.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==0 & F14.pf==0 & F15.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==0 & F14.pf==0 & F15.pf==0 & F16.pf==1)

bysort hid: egen lucke = max(luecken)
	lab var lucke "On-Off-Caring"
	lab val lucke yn

* gaps bigger than 1 year
sort pid syear
bysort pid: gen lucke2plus = (pf==1 & F.pf==0 & F2.pf==0 & F3.pf==1 | ///
						  pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==1 | ///
				pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==1 | ///
	  pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==1 | ///  
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==0 & F14.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==0 & F14.pf==0 & F15.pf==1 | ///
pf==1 & F.pf==0 & F2.pf==0 & F3.pf==0 & F4.pf==0 & F5.pf==0 & F6.pf==0 & F7.pf==0 & F8.pf==0 & F9.pf==0 & F10.pf==0 & F11.pf==0 & F12.pf==0 & F13.pf==0 & F14.pf==0 & F15.pf==0 & F16.pf==1)	
bysort hid: egen lucken2plus = max(lucke2plus)
	lab var lucken2plus "On-Off-Caring | gaps>1 year"
	lab val lucken2plus yn
* drop
drop if lucken2plus==1
	
** help variables for time of caring (caretaker)
* 1st year of caregiving
sort hid pid syear
bysort pid: egen min_pfid = min(syear) if gepflegter_id == pid 
	lab var min_pfid "1st year of need of care"
	* HH level
	bysort hid: egen min_pfid_hh = min(min_pfid)
		lab var min_pfid_hh "1st year of need of care"
			bysort hid: egen min_pfid_hh1 = max(min_pfid)
	
* last year of caregiving
sort hid pid syear
bysort pid: egen max_pfid = max(syear) if gepflegter_id == pid 
	lab var max_pfid "last year of need of care"
	* HH level
	bysort hid: egen max_pfid_hh = max(max_pfid)
		lab var max_pfid_hh "last year of need of care"

		bysort hid: egen max_pfid_hh1 = min(max_pfid)

* identify caregiver
* link to PNR - caregiver
sort hid pid syear 
gen pfleger_id = pid if pfleger_pnr==pnrakt & pfleger_pnr!=0
* fill gaps
sort pid syear
bysort pid: replace pfleger_id = pid if L.pfleger_id!=. & F.pfleger_id!=. ///
			& L.pfleger_id==pid & F.pfleger_id==pid
* drop HH with more than one caregiver
unique pfleger_id, by(hid) gen(mehrere)
bysort hid: egen mehr = min(mehrere)
keep if mehr<3

* harmonize caregiver within HH 
bysort hid: egen pfleger_all = min(pfleger_id) if nevercare==0
bysort hid: egen pfleger_all1 = max(pfleger_id) if nevercare==0
	
** help variables for time of caring (caregiver)
* 1st year of caregiving
bysort pid: egen min_pflid = min(syear) if pfleger_id==pid 
	lab var min_pflid "1st year of caregiving"
	* HH level
	bysort hid: egen min_pflid_hh = min(min_pflid) if soep_anfang<min_pflid
		lab var min_pflid_hh "1st year of caregiving"

* last year of caregiving
bysort pid: egen max_pflid = max(syear) if pfleger_id==pid  
	lab var min_pflid "last year of caregiving"
	* HH level
	bysort hid: egen max_pflid_hh = max(max_pflid) 
		lab var max_pflid_hh "last year of caregiving"

* duration of caregiving I	
gen dauer_mitluecken=max_pflid_hh-min_pflid_hh+1
gen dauer_echt=max_pflid_hh-min_pflid_hh+1 if lucke==0
* duration of need of care I
gen dauer_pflegebed = max_pfid_hh - min_pfid_hh +1
	lab var dauer_pflegebed "duration of need of care"
bysort hid: egen dauer_pflegefall= min(dauer_pflegebed)
	lab var dauer_pflegefall "duration of need of care"
* duration of caregiving II
gen dauer_cg = max_pflid_hh - min_pflid_hh +1
	lab var dauer_cg "duration of caregiving"
bysort hid: egen dauer_care= min(dauer_cg)
	lab var dauer_care "duration of caregiving"

** relationship between caregiver and caretaker
* Dummy for caregivers and caretaker (only during caring years)
gen giver = (pfleger_id==pid) 
gen taker = (gepflegter_id==pid) 				
	lab var giver "caregiver"
	lab var taker "caretaker"
gen giver_imm = (pfleger_all==pid)
gen taker_imm = (gepflegter_all==pid)

* Dummy if caretaker or caregiver is the head of the household
	bysort hid: gen hhv = (stell==0)
	bysort pid: gen hhv_g = (hhv==1 & gepflegter_all==pnrakt)
	bysort pid: gen hhv_p = (hhv==1 & pfleger_all==pid)

* when caretaker is the head of the household
gen pfleger_ver = 9 if hhv==1 & taker==1
	bysort hid: egen helpi1 = min(pfleger_ver)
replace pfleger_ver = 1 if stell==30 & giver_imm==1	& helpi1==9 // Child
replace pfleger_ver = 1 if stell==31 & giver_imm==1	& helpi1==9 // Child
replace pfleger_ver = 2 if stell==20 & giver_imm==1	& helpi1==9 // Parent
replace pfleger_ver = 2 if stell==21 & giver_imm==1	& helpi1==9 // Parent
replace pfleger_ver = 2 if stell==22 & giver_imm==1	& helpi1==9 // Parent
replace pfleger_ver = 3 if stell==11 & giver_imm==1	& helpi1==9 // Partner
replace pfleger_ver = 3 if stell==12 & giver_imm==1	& helpi1==9 // Partner
replace pfleger_ver = 3 if stell==13 & giver_imm==1	& helpi1==9 // Partner
replace pfleger_ver = 4 if stell==25 & giver_imm==1	& helpi1==9 // Grandparent
replace pfleger_ver = 5 if stell==27 & giver_imm==1	& helpi1==9 // Parent in law
replace pfleger_ver = 6 if stell==35 & giver_imm==1	& helpi1==9 // Child in law
replace pfleger_ver = 7 if stell==40 & giver_imm==1	& helpi1==9 // Sibling
replace pfleger_ver = 7 if stell==41 & giver_imm==1	& helpi1==9 // Sibling
replace pfleger_ver = 7 if stell==52 & giver_imm==1	& helpi1==9 // Sibling
replace pfleger_ver = 8 if stell==71 & giver_imm==1	& helpi1==9 // Others
replace pfleger_ver = 8 if stell==60 & giver_imm==1	& helpi1==9 // Others
replace pfleger_ver = 8 if stell==63 & giver_imm==1	& helpi1==9 // Others
replace pfleger_ver = 8 if stell==64 & giver_imm==1	& helpi1==9 // Others
lab var pfleger_ver "Relationship caregiver and -taker: who is the caretaker?"

* when caregiver is the head of the household
	gen helpi2_vor = (hhv==1 & giver==1)
	bysort hid: egen helpi2 = max(helpi2_vor)
replace pfleger_ver = 2 if stell==30 & taker_imm==1	& helpi2==1 // Parent
replace pfleger_ver = 2 if stell==31 & taker_imm==1	& helpi2==1 // Parent
replace pfleger_ver = 1 if stell==20 & taker_imm==1	& helpi2==1 // Child
replace pfleger_ver = 1 if stell==21 & taker_imm==1	& helpi2==1 // Child
replace pfleger_ver = 1 if stell==22 & taker_imm==1	& helpi2==1 // Child
replace pfleger_ver = 3 if stell==11 & taker_imm==1	& helpi2==1 // Partner
replace pfleger_ver = 3 if stell==12 & taker_imm==1	& helpi2==1 // Partner
replace pfleger_ver = 3 if stell==13 & taker_imm==1	& helpi2==1 // Partner
replace pfleger_ver = 6 if stell==27 & taker_imm==1	& helpi2==1 // Child in law
replace pfleger_ver = 5 if stell==35 & taker_imm==1	& helpi2==1 // Parent in law
replace pfleger_ver = 7 if stell==40 & taker_imm==1	& helpi2==1 // Sibling
replace pfleger_ver = 7 if stell==41 & taker_imm==1	& helpi2==1 // Sibling
replace pfleger_ver = 7 if stell==52 & taker_imm==1	& helpi2==1 // Sibling
replace pfleger_ver = 8 if stell==71 & taker_imm==1	& helpi2==1 // Other
replace pfleger_ver = 8 if stell==60 & taker_imm==1	& helpi2==1 // Other
replace pfleger_ver = 8 if stell==63 & taker_imm==1	& helpi2==1 // Other
replace pfleger_ver = 8 if stell==64 & taker_imm==1	& helpi2==1 // Other

* help-variables: who is it in relationship to the head of the household?
	foreach x in giver_imm taker_imm {
		gen 	`x'_eltern = (`x'==1 & stell==30)
		replace `x'_eltern = 1 if `x'==1 & stell==31

		gen 	`x'_kind = (`x'==1 & stell==20)
		replace `x'_kind = 1 if `x'==1 & stell==21
		replace `x'_kind = 1 if `x'==1 & stell==22

		gen 	`x'_partner = (`x'==1 & stell==11)
		replace `x'_partner = 1 if `x'==1 & stell==12
		replace `x'_partner = 1 if `x'==1 & stell==13

		gen 	`x'_enkel = (`x'==1 & stell==25)

		gen 	`x'_schwiegerkind = (`x'==1 & stell==27)

		gen 	`x'_schwiegerelt = (`x'==1 & stell==35)

		gen 	`x'_geschwister = (`x'==1 & stell==40)
		replace `x'_geschwister = 1 if `x'==1 & stell==41
		replace `x'_geschwister = 1 if `x'==1 & stell==52

		gen 	`x'_sonstige = (`x'==1 & stell==71)
		replace `x'_sonstige = 1 if `x'==1 & stell==60
		replace `x'_sonstige = 1 if `x'==1 & stell==63
		replace `x'_sonstige = 1 if `x'==1 & stell==64
}
* harmonize within the household
global verh1 "eltern kind partner enkel schwiegerkind" 
global verh2 "schwiegerelt geschwister sonstige"
foreach x in $verh1 $verh2 {
	bysort hid: egen t_`x' = max(taker_imm_`x')
}
foreach x in $verh1 $verh2 {
	bysort hid: egen g_`x' = max(giver_imm_`x')
}
* Partner
replace pfleger_ver = 3 if g_eltern==1 		  & t_eltern==1 		& hhv==0
replace pfleger_ver = 3 if g_kind==1 		  & t_schwiegerkind==1
replace pfleger_ver = 3 if g_schwiegerkind==1 & t_kind==1
* Parents as caretaker
replace pfleger_ver = 2 if g_geschwister==1   & t_eltern==1	
replace pfleger_ver = 2 if g_kind==1 		  & t_partner==1
replace pfleger_ver = 2 if g_schwiegerkind==1 & t_sonstige==1
replace pfleger_ver = 2 if g_partner==1 	  & t_schwiegerelt==1
* Parents in law as caretaker
replace pfleger_ver = 5 if g_schwiegerkind==1 & t_partner==1
replace pfleger_ver = 5 if g_partner==1 	  & t_eltern==1
* Child as caretaker
replace pfleger_ver = 1 if g_partner==1 & t_kind==1
	lab define pfleger_ver 			///
		2 "Parent" 					///
		1 "Child"					///
		3 "Partner"					///
		4 "Grandchild"				///
		6 "Child in law"			///
		5 "Parent in law"			///
		7 "Sibling (in law)"		///
		8 "Others"					///
		9 "no answer"						
	lab val pfleger_ver pfleger_ver

* harmonize within the household
bysort hid: egen pfleger_verh = min(pfleger_ver)
lab val pfleger_verh pfleger_ver

* keep all HH with partner caring and all HH without caring at all
keep if pfleger_verh==3	| nevercare==1

* year of death of caretaker
gen tod_gepfl_helpi = todjahr if gepflegter_id==pid
bysort hid: egen tod_gepflegter = min(tod_gepfl_helpi)

* year of death of caregiver
gen tod_pfleg_helpi = todjahr if pfleger_id==pid
bysort hid: egen tod_pfleger = max(tod_pfleg_helpi)
	
* left and right censured cases	
gen survey_anfang = (soep_anfang==min_pflid_hh | soep_anfang==min_pfid_hh)
	lab var survey_anfang "Survey and Caring start at the same time"
	lab val survey_anfang yn

gen survey_ende   = (soep_ende  ==max_pflid_hh | soep_ende  ==max_pfid_hh)
	lab var survey_ende "Survey and Caring end at the same time"
	lab val survey_ende yn

** time variables
* event variable 
* start of caregiving
gen start = (min_pflid_hh==syear & pfleger_id!=. & survey_anfang==0)
	lab var start "Start"
* end of caregiving
gen ende = (max_pflid_hh+1==syear & pfleger_all==pid & pf==0)
	lab var ende "End"

* years around the start	
foreach x of numlist 1/3 {
	bysort pid: gen before`x'_start = (syear + `x' == min_pflid_hh)
	bysort pid: gen after`x'_start 	= (syear - `x' == min_pflid_hh)
}
bysort pid: gen before4_start = (syear + 4 <= min_pflid_hh)
bysort pid: gen after4_start  = (syear - 4 >= min_pflid_hh)
replace         after4_start  = 0 if pf==0

foreach x of numlist 1/4 {
	replace after`x'_start = 0 if max_pflid_hh <= syear - `x' 
}

foreach x of numlist 1/4 {
	lab var before`x'_start "-`x' Y."
}
foreach x of numlist 1/4 {
	lab var after`x'_start "+`x' Y."
}
gen absolut_a = 0 if start==1
foreach x of numlist 1/4 {
	replace absolut_a = -`x' if before`x'_start==1
	replace absolut_a =  +`x' if after`x'_start==1
}
replace absolut_a =  . if survey_anfang==1 | nevercare==1
	lab var absolut_a "Years around the caregiving start"

	** for CI-Plot: years around the caregiving start (-3 years, +5 years)
	gen Pflegestart 	= absolut_a
	replace Pflegestart = . if absolut_a <= -3 | nevercare==1 ///
						  | survey_anfang==1
	replace Pflegestart = 4 if absolut_a >= 5
		lab var Pflegestart 	
			lab define jahre ///
				-2 "-2" 	 ///
				-1 "-1" 	 ///
				 0 "Begin" 	 ///
				 1 "1" 		 ///
				 2 "2" 		 ///
				 3 "3" 		 ///
				 4 "4+" 		 	 
		lab val Pflegestart jahre
		label define Pflegestart 0 "Start"
	label value Pflegestart Pflegestart

* years around the end
sort pid syear
foreach x of numlist 1/3 {
	bysort pid: gen before`x'_end = (syear -1 + `x' == max_pflid_hh & syear -1 +`x' >=min_pflid_hh & F`x'.ende==1)
	bysort pid: gen after`x'_end  = (syear -1 - `x' == max_pflid_hh & syear -1 -`x' >=min_pflid_hh & L`x'.ende==1)
}
	bysort pid: gen before4_end	  = (syear +3 <= max_pflid_hh & syear +3 >=min_pflid_hh & L.before3_end==1)
	bysort pid: gen after4_end	  = (syear -5 == max_pflid_hh & syear -5 >=min_pflid_hh & L.after3_end==1)

** if the last year of caregiving is the beginning of survey start, -1 was not generatet. 
*Therefore:
bysort pid: replace before1_end = 1 if syear==max_pflid_hh & survey_anfang==1 & F.ende==1
	
** for controls
foreach x of numlist 1/4 {
	replace before`x'_end = 0 if pfleger_all!=pid
}
foreach x of numlist 1/4 {
	replace before`x'_end = 0 if dauer_echt<`x'
}
foreach x of numlist 1/4 {
	lab var before`x'_end "-`x' J."
}
foreach x of numlist 1/4 {
	lab var after`x'_end  "+`x' J."
}
gen absolut_e = 0 if ende==1
foreach x of numlist 1/4 {
	replace absolut_e = -`x' if before`x'_end==1
	replace absolut_e =  `x' if after`x'_end==1
}
replace absolut_e =  . if survey_ende==1
	lab var absolut_e "ears around the end"

	** for CI-Plot: years around the caregiving end (-3 years, +5 years)
	gen Pflegeende	 	= absolut_e
	replace Pflegeende	= -3 if absolut_e < -3
	replace Pflegeende	= . if Pflegeende==-3
	replace Pflegeende	= . if absolut_e > 4
		lab var Pflegeende 	
			lab define jahre_e 	///
				-2 "-2" 	 	///
				-1 "-1" 	 	///
				 0 "End" 	 	///
				 1 "1" 		 	///
				 2 "2" 		 	///
				 3 "3" 		 	///
				 4 "4+" 		 	 
		lab val Pflegeende jahre_e


* identifyer for years before and during caregiving
gen zeit_pfl_a = (syear<=max_pflid_hh) if nevercare==0
	lab var zeit_pfl_a "years before and during caregiving"
* identifyer for years during and after caregiving
gen zeit_pfl_e = (syear>=min_pflid_hh) if nevercare==0
	lab var zeit_pfl_e "years during and after caregiving"

	lab var dauer_echt "Pflegedauer insgesamt"
	
* Reason why Caregiving ends	
	* Death
	fre pld0146 if pfleger_verh==3	// 224 cases
gen partner_tod = (pld0146==1 & pfleger_verh==3 & max_pflid_hh==syear-1 & pfleger_all==pid)
	// first year after caregiving

gen tod_helpi_gepf = (todjahr==syear & max_pflid_hh==syear & gepflegter_all==pid)
	// when death of care recipient was inserted afterwards into the SOEP
	// year of death of care recipient; therefore the last year of caregiving
gen tod_helpi_gepf2 = todjahr+1 if todjahr==syear & max_pflid_hh==syear & gepflegter_all==pid
 
 
* harmonize the variables above
gen partner_tod_jahr=todjahr1 
replace partner_tod_jahr=todjahr2 if partner_tod_jahr==.
replace partner_tod_jahr=tod_helpi_gepf2 if partner_tod_jahr==.

* variable only for care recipient´s death (partner caregiving)
gen partner_tod_event = (partner_tod_jahr==syear & pfleger_verh==3 & pfleger_all==pid & max_pflid_hh==syear-1)
fre partner_tod_event	// 157 cases
	lab var partner_tod "Death of the Care Recipient"
	lab val partner_tod yn

* Recovery	
* Year when caregiving ends
gen helpi_jahr_ende = syear if Pflegeende==0
bysort hid: egen jahr_ende = max(helpi_jahr_ende)

* when care recipient is still in the HH, but no more in need
sort pid syear
bysort pid: gen recovery = (pf==0 & pfleger_verh==3 & L.pf==1 & gepflegter_all==pid & jahr_ende==syear)	

* only in recovery status as long (former) care recipient is alive	
bysort hid: egen recover_reg = max(recovery)
sort pid syear
bysort pid:	replace recover_reg = 0 if partner_tod==1 | L.partner_tod==1 | 	///
				 L2.partner_tod==1 | L3.partner_tod==1 | L4.partner_tod==1 | ///
				 L5.partner_tod==1 | L6.partner_tod==1 | L7.partner_tod==1 | ///
				 L8.partner_tod==1 | L9.partner_tod==1 | L10.partner_tod==1 | ///	
				 L9.partner_tod==1 | L10.partner_tod==1 | L11.partner_tod==1 | ///	
				L12.partner_tod==1 | L13.partner_tod==1 | L14.partner_tod==1 | ///	
				L15.partner_tod==1 | L16.partner_tod==1 	

fre recover_reg if ende==1			// 111 cases
fre partner_tod_event if ende==1	// 157 cases

// was ist mit den Personen, die auf beiden den Wert 0 haben?
	
* harmonize within the household
bysort hid: egen tod_helpi = max(partner_tod_event) if pfleger_all==pid
bysort hid: egen recov_helpi = max(recover_reg) if pfleger_all==pid

* Reason for ending caregiving within 1 variable
gen grund = 1 if tod_helpi==1
replace grund = 2 if recov_helpi==1
replace grund = 3 if tod_helpi==0 & recov_helpi==0
	lab var grund "Reason End of Caregiving"
		lab define grund				///
			1 "Death of Care Recipient"	///
			2 "Recovery of Care Recipient"				
	lab val grund grund

* drop further persons within the caregiving HH
gen keinpfleger = (pfleger_verh==3 & pfleger_all!=pid)
drop if keinpfleger==1		
	
* Sub-Dimensions * satisfied with...
clonevar geld = plh0175
clonevar geld_eigen = plh0176 
clonevar family = plh0180
clonevar health = plh0171 
clonevar schlaf = plh0172
clonevar arbeit = plh0173
clonevar wohnung = plh0177
clonevar friends = plh0181
clonevar hh = plh0174
clonevar freizeit = plh0178

* impute missings
mean hh
gen no_hh=(hh>10) 
clonevar hh_imputation = hh
replace hh_imputation = 6.748594 if no_hh==1

mean household_income
gen no_hh_inc =(household_income>10)
clonevar hh_inc_imputation = household_income
replace hh_inc_imputation = 6.449501 if no_hh_inc==1

mean leisure
gen no_freizeit =(leisure>10)
clonevar leisure_imputation = leisure
replace leisure_imputation = 7.03184  if no_freizeit==1

* variable for all those cases to integrate easier in the modells
gen modell = (hh_imputation<11 & leisure_imputation<11 & health<11 & ///
			  hh_inc_imputation<11 & pf<2)
			  


capture rename age alter
capture egen mean_age=mean(alter)
capture gen age = alter - mean_age
capture drop mean_age

rename arbeit work
rename wohnung housing
rename geld household_income
rename geld_eigen own_income
rename freizeit leisure
rename schlaf sleep


* Regressions and Graphs
*** Transitions into caregiving
** Table 3
* Model 1: Random Effects
xtreg happy pf age if modell==1, re mle
est store re

* Model 2: Fixed Effects
xtreg happy pf age if modell==1, robust fe
est store fe

* Model 3: Impact Function (FE)
xtreg happy  start after1_start after2_start after3_start after4_start	///
                  age if (zeit_pfl_a==1 | nevercare==1) & modell==1, robust fe 
est store IF_all

* Model 4: Impact Function (FE) + health
xtreg happy start after1_start after2_start after3_start after4_start       ///
                  age health if (zeit_pfl_a==1 | nevercare==1) & modell==1, robust fe 
est store IF_health

* Model 5: Impact Function (FE) + hh_income
xtreg happy start after1_start after2_start after3_start after4_start       ///
                  age hh_inc_imputation no_hh_inc if (zeit_pfl_a==1 | nevercare==1) & modell==1, robust fe 
est store IF_household_income

* Model 6: Impact Function (FE) + leisure
xtreg happy start after1_start after2_start after3_start after4_start       ///
                  age leisure_imputation no_freizeit if (zeit_pfl_a==1 | nevercare==1) & modell==1, robust fe 
est store IF_leisure

* Model 7: Impact Function (FE) + hh-tasks
xtreg happy start after1_start after2_start after3_start after4_start       ///
                  age hh_imputation no_hh if (zeit_pfl_a==1 | nevercare==1) & modell==1, robust fe 
est store IF_hh

* Model 8: Impact Function (FE) + all subdimensions
xtreg happy start after1_start after2_start after3_start after4_start       ///
                  age health hh_inc_imputation leisure_imputation hh_imputation ///
				  no_hh_inc no_freizeit no_hh	///
                  if (zeit_pfl_a==1 | nevercare==1) & modell==1, robust fe 
est store IF_full

esttab re fe IF_all IF_health IF_household_income IF_leisure IF_hh IF_full using table_3_into.rtf, stats(ll aic r2_b r2_w r2_o N N_g)

* Figure 2
foreach var of varlist health work housing household_income own_income leisure family sleep friends{
quietly: xtreg  `var' start after1_start after2_start after3_start after4_start ///
                age if zeit_pfl_a==1 | nevercare==1, robust fe
quietly:  estat ic

est store IF_`var'

coefplot IF_`var', ///
keep(start after1_start after2_start after3_start after4_start) vertical recast(line) lcolor(black) lwidth(thick)   ///
     ciopts(recast(rline) lcolor(black)  lpattern(dash) lwidth(medthick) )                                    ///
     ylabel(-2.75(.5).5, grid angle(0) labsize(small) format(%3.2f))   ///
     coeflabels(after1_start="1" after2_start="2" after3_start="3"                                              ///
                after4_start="4+") mcolor(black) xlabel(,labsize(small))                               ///
                               title("`var'" " ", size(medium) color(black))                                                                         ///
                graphregion(fcolor(white) lcolor(white) ifcolor(white)                                                  ///
                ilcolor(white)) saving(transition_into_`var') ///
                yline(0, lcolor(red) lpattern(dash) lwidth(medthick))
}
graph combine 	transition_into_health.gph transition_into_sleep.gph transition_into_housing.gph ///
				transition_into_work.gph transition_into_own_income.gph transition_into_household_income.gph  ///
                transition_into_leisure.gph transition_into_friends.gph transition_into_family.gph,   ///
				graphregion(fcolor(white) lcolor(white) ilcolor(white) ifcolor(white)) ///
				ysize(12) xsize(9) rows(3) saving(figure_2_into, replace) 
*title("Transition into caregiving")

*** Transitions into caregiving
** Table 4
gen out_of = (pf - 1)*-1

* Model 1: Random Effects
xtreg happy out_of age if (pfleger_all==pid & zeit_pfl_e==1) | nevercare==1  & modell==1, re mle
est store re

* Model 2: Fixed Effects
xtreg happy out_of age if (pfleger_all==pid & zeit_pfl_e==1) | nevercare==1 & modell==1, fe
est store fe

* Model 3: Impact Function (FE)
xtreg happy ende after1_end after2_end after3_end after4_end             ///
                  age if (pfleger_all==pid & zeit_pfl_e==1) | nevercare==1 & modell==1, robust fe           
est store IF_all

* Model 4: Impact Function (FE) | sorted by reason
capture gen tod=(grund==1)
capture gen erholung=(grund==2)

capture gen before_end_tod = tod*before1_end
capture gen ende_tod = tod*ende
capture gen after1_end_tod = tod*after1_end
capture gen after2_end_tod = tod*after2_end
capture gen after3_end_tod = tod*after3_end
capture gen after4_end_tod = tod*after4_end

capture gen before_end_recovery = erholung*before1_end
capture gen ende_recovery = erholung*ende
capture gen after1_end_recovery = erholung*after1_end
capture gen after2_end_recovery = erholung*after2_end
capture gen after3_end_recovery = erholung*after3_end
capture gen after4_end_recovery = erholung*after4_end

xtreg happy 	ende_recovery after1_end_recovery after2_end_recovery after3_end_recovery after4_end_recovery ///
                ende_tod after1_end_tod after2_end_tod after3_end_tod after4_end_tod ///
                age if (pfleger_all==pid & zeit_pfl_e==1) | nevercare==1 & modell==1, robust fe
est store IF_type

* Graphs: Separately by Death/Recovery
foreach var of varlist health work housing household_income own_income leisure family sleep friends{
quietly: xtreg  `var'          before_end_recovery ende_recovery after1_end_recovery after2_end_recovery after3_end_recovery after4_end_recovery ///
                               before_end_tod ende_tod after1_end_tod after2_end_tod after3_end_tod after4_end_tod ///
                               age if (pfleger_all==pid & zeit_pfl_e==1) | nevercare==1 , robust fe

quietly:  estat ic
est store IF_type_`var'

coefplot IF_type_`var', ///
keep(before_end_recovery ende_recovery after1_end_recovery after2_end_recovery after3_end_recovery after4_end_recovery) vertical recast(line) lcolor(black)  lwidth(thick)                                                  ///
     ciopts(recast(rline) lcolor(black)  lpattern(dash) lwidth(medthick) )                                    ///
     ylabel(-2(.5)2, grid angle(0) labsize(small) format(%3.2f))   ///
     coeflabels(before_end_recovery="-1" ende_recovery="0"  after1_end_recovery="1" after2_end_recovery="2" after3_end_recovery="3"                                       ///
                after4_end_recovery="4+") mcolor(black) xlabel(,labsize(small))                                            ///
                title("`var'" " ", size(medium) color(black))                                                                         ///
                graphregion(fcolor(white) lcolor(white) ifcolor(white)                                                  ///
                ilcolor(white)) saving(recovery_`var', replace) ///
                yline(0, lcolor(red) lpattern(dash) lwidth(medthick))
           
coefplot IF_type_`var', ///
keep(before_end_tod ende_tod after1_end_tod after2_end_tod after3_end_tod after4_end_tod) vertical recast(line) lcolor(black)  lwidth(thick)                                                     ///
     ciopts(recast(rline) lcolor(black)  lpattern(dash) lwidth(medthick) )                                    ///
     ylabel(-2(.5)2, grid angle(0) labsize(small) format(%3.2f))   ///
     coeflabels(before_end_tod="-1" ende_tod="0" after1_end_tod="1" after2_end_tod="2" after3_end_tod="3"                                    ///
                after4_end_tod="4+") mcolor(black) xlabel(,labsize(small))                                       ///
                title("`var'" " ", size(medium) color(black))                                                                         ///
                graphregion(fcolor(white) lcolor(white) ifcolor(white)                                                  ///
                ilcolor(white)) saving(death_`var', replace) ///
                yline(0, lcolor(red) lpattern(dash) lwidth(medthick))
}

graph combine 	death_health.gph death_sleep.gph death_housing.gph ///
				death_work.gph death_own_income.gph death_household_income.gph ///
                death_leisure.gph death_friends.gph death_family.gph,     ///                                          ///
                graphregion(fcolor(white) lcolor(white) ilcolor(white) ifcolor(white)) ///
                ysize(8) rows(4) saving(death, replace) title("a. Death of partner", size(medium) color(black))
graph combine 	recovery_health.gph recovery_sleep.gph recovery_housing.gph ///
				recovery_work.gph recovery_own_income.gph recovery_household_income.gph ///
				recovery_leisure.gph recovery_friends.gph recovery_family.gph, ///                                                    ///
                graphregion(fcolor(white) lcolor(white) ilcolor(white) ifcolor(white)) ///      
				ysize(8) rows(4) saving(recovery, replace) title("b. Recovery of partner", size(medium) color(black))
graph combine death.gph recovery.gph , saving(figure_3_out, replace) rows(1) ysize(12) xsize(20) graphregion(fcolor(white) lcolor(white) ilcolor(white) ifcolor(white))


* Table 6
* Tabelle A1:
* Korrelationsmatrix
pwcorr happy health leisure household_income hh, star(0.01) obs
eststo pw
esttab pw using pw.rtf, stats(N N_g) replace

corr happy health 
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr happy leisure 
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr happy household_income 
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr happy hh 
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr health leisure
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr health household_income
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr health hh
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr leisure household_income
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr leisure hh
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))

corr household_income hh
return list
di r(rho)/sqrt((1-r(rho)^2)/(r(N)-2))
